﻿$(document).ready(function() {

	var currentMonth;
	var currentMonthCategories;
	var appHeight

	$.namespace("global", {
		inherited: true,

		ApplicationHeight: function(value) {
			if (value) {
				appHeight = value;
			}
			else {
				return appHeight;
			}
		},

		CurrentMonth: function(value) {

			if (value) {
				currentMonth = value;
			}
			else {
				return currentMonth;
			}
		},

		CurrentMonthCategories: function(value) {

			if (value) {
				currentMonthCategories = value;
			}
			else {
				return currentMonthCategories;
			}
		},

		Months: function(isJson) {
			var mths;
			if (isJson) {
				mths = [
					{ 'MonthIndex': '1', 'MonthName': 'January' },
					{ 'MonthIndex': '2', 'MonthName': 'February' },
					{ 'MonthIndex': '3', 'MonthName': 'March' },
					{ 'MonthIndex': '4', 'MonthName': 'April' },
					{ 'MonthIndex': '5', 'MonthName': 'May' },
					{ 'MonthIndex': '6', 'MonthName': 'June' },
					{ 'MonthIndex': '7', 'MonthName': 'July' },
					{ 'MonthIndex': '8', 'MonthName': 'August' },
					{ 'MonthIndex': '9', 'MonthName': 'September' },
					{ 'MonthIndex': '10', 'MonthName': 'October' },
					{ 'MonthIndex': '11', 'MonthName': 'November' },
					{ 'MonthIndex': '12', 'MonthName': 'December' }
				];
			}
			else {
				mths = [
					['1', 'January'],
					['2', 'February'],
					['3', 'March'],
					['4', 'April'],
					['5', 'May'],
					['6', 'June'],
					['7', 'July'],
					['8', 'August'],
					['9', 'September'],
					['10', 'October'],
					['11', 'November'],
					['12', 'December']
				];
			}
			return mths;
		},

		MonthRenderer: function(value, metaData, record, rowIndex, colIndex, store) {
			if (value) {
				return $.fn.global.Months(true)[value - 1].MonthName;
			} else {
				return value;
			}
		},

		ComboRenderer: function(combo) {
			return function(value) {
				var record = combo.findRecord(combo.valueField, value);
				return record ? record.get(combo.displayField) : combo.valueNotFoundText;
			}
		},

		GetCurrentMonth: function() {
			$.ajax({
				type: "POST",
				url: 'Data/Months.aspx?xaction=getmonthbydate',
				contentType: "application/json;",
				data: JSON.stringify(new Date()),
				dataType: "json",
				async: false,
				success: function(msg) {
					if (true) { }
					$.fn.global.CurrentMonth(msg.SingleData);
				},
				error: function(msg) {
					$("#loading").text(msg.Data.Message);
				}
			});
		},

		/* Define the grid that will display the Mth Category summary */
		SummaryGrid: function(analysisDate, showRefreshBtn) {
			//proxy
			var proxy = new Ext.data.HttpProxy({
				url: 'Data/Spendings.aspx'
			});

			//reader
			var reader = new Ext.data.JsonReader({
				root: 'Data',
				totalProperty: 'Count',
				idProperty: 'MthCategoryId',
				fields: [
					{ name: 'MthCategoryId', type: 'int' },
					{ name: 'CategoryName', type: 'string' },
					{ name: 'AmountSpent', type: 'float' },
					{ name: 'BudgetAmount', type: 'float' }
				]
			});

			//store
			var store = new Ext.data.Store({
				id: 'dsMthSummary',
				proxy: proxy,
				reader: reader,
				//					sortInfo: {
				//						field: 'CategoryName',
				//						direction: 'ASC'
				//					},
				listeners: {
					'beforeload': function(store, options) {
						options.params['xaction'] = 'getmonthsummary';
						options.params['AnalysisDate'] = analysisDate;
					},
					'load': function(store, records, options) {
						if (true) { }
						if (true) { }
					}
				}
			});

			function RenderAmountSpent(value, metaData, record, rowIndex, colIndex, store) {
				var AmountSpent = record.data.AmountSpent;
				var BudgetAmount = record.data.BudgetAmount;

				if (AmountSpent > BudgetAmount) {
					metaData.css = 'summary-overspend';
				}
				else if (((BudgetAmount - AmountSpent) / BudgetAmount * 100) < 5) {
					metaData.css = 'summary-warning';
				}
				return Ext.util.Format.usMoney(value);
			};

			//column model
			var colModel = new Ext.grid.ColumnModel({
				//					defaults: {
				//						sortable: true
				//					},
				columns: [
					{ header: 'Category', dataIndex: 'CategoryName' },
					{ header: 'Budget Amount', dataIndex: 'BudgetAmount', align: 'right', renderer: 'usMoney' }, // xtype: 'numbercolumn', format: '$0,0.00' },
					{header: 'Amount Spent', dataIndex: 'AmountSpent', align: 'right', renderer: RenderAmountSpent }
				]
			});

			var refreshBtn = new Ext.Button({
				id: 'refreshBtn',
				text: 'Refresh',
				iconCls: 'silk-table-refresh',
				handler: function(btn, ev) {
					grid.getStore().rejectChanges();
					grid.getStore().reload();
				}
			});

			//grid
			var grid = new Ext.grid.GridPanel({
				id: 'MonthSummaryGrid',
				iconCls: 'icon-grid',
				frame: true,
				title: 'Spendings Summary',
				loadMask: true,
				autoScroll: true,
				height: 300,
				store: store,
				colModel: colModel,
				viewConfig: {
					forceFit: true
				},
				tbar: ['->', '-', refreshBtn]
			});

			if (!showRefreshBtn) {
				//				grid.getTopToolbar().setVisible(false);
				//				tbar.hide();
				//				grid.doLayout();
			}
			//			tbar.doLayout();
			//			grid.doLayout();
			return grid;
		}

	});
});
